using System.Collections.Generic;
using Itdavany.Objects;
using Itdavany.Data;


namespace Itdavany.Objects
{
    public class AnimationClip : Widget
    {
        double interval = 0;

        Animation anim;
        List<Scene> framelist;
        int curframecount;

        //public AnimationClip() { throw new System.Exception(); }


        public AnimationClip(Animation anim)
        {
            SetAnimation(anim);
        }


        public void SetAnimation(Animation anim)
        {
            this.anim = anim;
            framelist = new List<Scene>();
            framelist.AddRange(anim.Frames.Values);
            curframecount = 0;
            interval = 0;

            SubScene = framelist[0];
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {

            if (anim.looping || curframecount != anim.FrameCount)
            {
                interval += gameTime.ElapsedGameTime.TotalMilliseconds;

                if (interval >= anim.FrameTags[curframecount].delay)
                {
                    interval -= anim.FrameTags[curframecount].delay;
                    if (curframecount >= anim.FrameCount - 1)
                    {
                        if (anim.looping)
                        {
                            curframecount = 0;
                        }
                    }
                    else curframecount++;

                    SubScene = framelist[curframecount];
                }
            }

            base.Update(gameTime);
        }

        public override void Draw()
        {
            if (!IsHidden)
            {
            }

            base.Draw();
        }

    }
}